package com.sanjiahantang.youjiaclient.module.login.views.impl;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseBean;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.module.login.presenters.LoginPresenter;
import com.sanjiahantang.youjiaclient.module.login.views.LoginView;
import com.sanjiahantang.youjiaclient.utils.CheckUtil;
import com.sanjiahantang.youjiaclient.utils.KeyBoardUtils;

import java.util.Timer;
import java.util.TimerTask;

import static com.sanjiahantang.youjiaclient.config.StaticValue.RESPONSE_CODE_SUC;

/**
 * Created by sks on 2015/9/17.
 * 找回密码
 */
public class ForgetActivity extends BaseMvpActivity<LoginView,LoginPresenter> implements View.OnClickListener,
        LoginView{
    private EditText editUserName,editPassword,editCode;
    private String name,password,code;
    private TextView txtCode;
    private TextView btnSure;
    private int startime = 60;
    private   Timer timer;
    @Override
    public LoginPresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_forget);

        TextView title=(TextView)findViewById(R.id.title);
        title.setText("找回密码");
        findViewById(R.id.fl_Left).setOnClickListener(this);
        editUserName = (EditText) findViewById(R.id.editUserName);
        editPassword = (EditText) findViewById(R.id.editPassword);
        //editAgain = (EditText) findViewById(R.id.editAgain);
        editCode = (EditText) findViewById(R.id.editCode);
        //txtEmail = (EditText) findViewById(R.id.txtEmail);
        txtCode = (TextView) findViewById(R.id.txtCode);
        btnSure = (TextView) findViewById(R.id.btnSure);
        editUserName.addTextChangedListener(textWatcher);
        editPassword.addTextChangedListener(textWatcher);
        //editAgain.addTextChangedListener(textWatcher);
        editCode.addTextChangedListener(textWatcher);
        //txtEmail.addTextChangedListener(textWatcher);
        txtCode.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            name = editUserName.getText().toString().trim();
            password = editPassword.getText().toString().trim();
            //again = editAgain.getText().toString().trim();
            //email = txtEmail.getText().toString().trim();
            code = editCode.getText().toString().trim();
            if(!name.equals("") && !password.equals("")
                    //&& !again.equals("")
                    && !code.equals("")
                    //&& !email.equals("")
                    ){
//                btnSure.setTextColor(getResources().getColor(R.color.white));
//                btnSure.setBackgroundResource(R.drawable.login_on_bg);
                btnSure.setClickable(true);
                btnSure.setOnClickListener(ForgetActivity.this);
            }else{
//                btnSure.setTextColor(getResources().getColor(R.color.gary_text));
//                btnSure.setBackgroundResource(R.drawable.login_off_bg);
                btnSure.setClickable(false);
            }
        }
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable editable) {
        }
    };
    private void sendCode(){
        presenter.getRegisterCustomerPassWordCode($this,name,"");
    }

    private Handler han = new Handler() {
        public void handleMessage(Message msg) {
            Bundle bundle = msg.getData();
            int s = bundle.getInt("code");
            if (s < 0) {
                timer.cancel();
                txtCode.setClickable(true);
                txtCode.setText("获取验证码");
                txtCode.setTextColor(getResources().getColor(R.color.colorPrimary));
                txtCode.setBackgroundResource(R.drawable.x_shape_green_border);
                startime = 60;
            } else {
                txtCode.setText("剩余时间:" + s);
                txtCode.setTextColor(getResources().getColor(R.color.gray));
                txtCode.setClickable(false);
                txtCode.setBackgroundResource(R.drawable.x_shape_gray_border_r20);
            }
        }

    };
    private void submitData(){
//        if(!password.equals(again)){
//            Utils.showToast(context,"两次输入的密码不一致");
//            return;
//        }
        presenter.findPassword($this,name,password, code);
    }

    public void getCode(BaseBean response) {
        if(response.getCode() == RESPONSE_CODE_SUC){
            txtCode.setTextColor(getResources().getColor(R.color.gray));
            txtCode.setBackgroundResource(R.drawable.x_shape_gray);
            txtCode.setClickable(false);
        }
        Utils.showToast($this,response.getMessage());
    }

    public void getData(BaseBean model) {
        if(model.getCode() == RESPONSE_CODE_SUC){
            finish();
        }
        Utils.showToast($this,model.getMessage());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_Left:
                KeyBoardUtils.closeKeybord(this);
                break;
            case R.id.txtCode:
                if (null == name || !CheckUtil.isMobileNO(name)) {
                    Utils.showToast($this, "请输入正确的手机号");
                    return;
                } else {
                    timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            startime--;
                            Bundle bundle = new Bundle();
                            bundle.putInt("code", startime);
                            Message mag = new Message();
                            mag.setData(bundle);
                            han.sendMessage(mag);
                        }
                    }, startime, 1000);
                }
                sendCode();
                break;
            case R.id.btnSure:
                submitData();
                break;
        }
    }

    @Override
    public void showProgress() {
        super.showProgress($this);
    }
}
